<?php

class FieldManager {

    private static $me = NULL;

    /**
     *
     * @return TableManger
     */
    public static function getInstance() {

        if (!self::$me instanceof FieldManager) {
            self::$me = new FieldManager();
        }

        return self::$me;
    }

    public function __construct() {
        NULL;
    }

    public function addField() {
        try {


            $name = $_REQUEST['field_name'];
            $position = $_REQUEST['field_position'];
            $length = $_REQUEST['field_length'];
            $type = $_REQUEST['field_type'];
            $description = $_REQUEST['field_description'];
            $isPrimary = $_REQUEST['field_primary'];
            $tableID = $_REQUEST['table_id'];

            $field = new Field();
            $field->setName($name);
            $field->setPosition($position);
            $field->setLength($length);
            $field->setType($type);
            $field->setDescription($description);
            $field->setIsPrimary($isPrimary);
            $field->setTableID($tableID);
            $field->add();
            $success = true;
            $message = 'Information has been added successfully';
            $content ['field_id'] = $field->getID();
        } catch (Exception $e) {
            $success = false;
            $message = $e->getMessage();
            $content = '';
        }
        return Utility::buildSuccessFailXmlResponse($success, $message, $content);
    }

    public function updateField() {



        try {
            $fieldID = $_REQUEST['field_id'];

            $name = $_REQUEST['field_name'];
            $position = $_REQUEST['field_position'];
            $length = $_REQUEST['field_length'];
            $type = $_REQUEST['field_type'];
            $description = $_REQUEST['field_description'];
             $isPrimary = $_REQUEST['field_primary'];



            $field = new Field($fieldID);
            $field->setName($name);
            $field->setPosition($position);
            $field->setLength($length);
            $field->setType($type);
            $field->setDescription($description);
            $field->setIsPrimary($isPrimary);

            $field->update();

            $success = true;
            $message = 'Information has been updated successfully';
            $content ['fieldID'] = $field->getID();
        } catch (Exception $e) {
            $success = false;
            $message = $e->getMessage();
            $content = '';
        }
        return Utility::buildSuccessFailXmlResponse($success, $message, $content);
    }

    public function deleteField() {
        try {

            $fieldID = $_REQUEST['field_id'];
            $field = new Field($fieldID);
            $field->delete();
            $success = true;
            $message = 'Information has been deleted successfully';
        } catch (Exception $e) {
            $success = false;
            $message = $e->getMessage();
            $content = '';
        }
        return Utility::buildSuccessFailXmlResponse($success, $message, $content);
    }

    public function getEmptyFieldView($tableID=0) {
        return TableEditView::getInstance()->getEmptyFieldRowTemplate();
    }

}

?>
